import controlP5.*;
import processing.serial.*;

// state constants
final int EATING		= 0;
final int EATEN		= 1;
final int IDLE		= 2;

DataStore		realFearMap = null;

/**
  * Represents a Food Patch
  */
public class FoodPatch {
  
	// 0 Eating, 1 Ate, 2 Idle
	private int state;
	  
	// position, in fear grid coordinates
	private int x;
	private int y;
	  
	// other stuff
	private color theColor;
	private int id;
	private boolean visible;
	private boolean available;
	private boolean hasFood;
	private int eaterCount;
	
	private float foodLeft;
        private float foodRefill;
  
	PFont theFont;
  	public FoodPatch(int x, int y, color _color, int _id) 
	{
		this.x = x;
		this.y = y;
		this.id = _id;
			  
		this.theColor = _color;
		this.visible = false;
		this.available = true;
		this.hasFood = false;
		this.eaterCount = 0;
		
		this.foodLeft = 0.0;
		this.foodRefill = 100.0;
		
		this.theFont = createFont("HelvetivaBold", 16.0);
		
		if (realFearMap == null)
		{
			realFearMap = new DataStore();
			realFearMap.loadFile(FEAR_MAP_FILE);
		}
	}
  
	public boolean isAvailable() { return available; }
	public boolean isVisible() { return visible; }
	public boolean hasEaten() { return foodLeft < 100.0; }

	public void refill(int _x, int _y)
	{
		this.x = _x;
		this.y = _y;
			  
		foodLeft = foodRefill;
		visible = true;
		available = false;
		hasFood = true;
		setIdle();
	}
	  
	public void startEat()
	{
		setEating();
		eaterCount++;
                println("Eating from " + id);
	}
	  
	public void endEating()
	{
		if ( hasFood )
		{
			foodLeft = average(realFearMap.getCell( y , x )) + random(0.0, 20.0) - 10.0;
			foodLeft = max(foodLeft, 0.0);
			foodLeft = min(foodLeft, 100.0);
			
			//foodLeft = 10.0 * random(2.0, 10.0);
			hasFood = false;
		}
		
		eaterCount--;
		if (eaterCount == 0)
		{
			setAte();
		}
	}
	  
	public boolean hasAnyFood()
	{
		return hasFood;
	}
	
	public float getLeftOver()
	{
		return foodLeft;
	}

        public void setRefillAmount(float foodRefill) 
        {

        		this.foodRefill = foodRefill;
        		setIdle();
        }


	public void draw() 
	{
		if (!visible) 
		{
			return;
		}
		
		
		PVector cell = environment.getGridCellCenter( new IVector(x, y) );
		pushStyle();
		{
			fill(theColor);
			stroke(0);
			strokeWeight(1.5);
			ellipse(cell.x, cell.y, FOOD_PATCH_SIZE, FOOD_PATCH_SIZE);
			
			if ( !hasFood )
			{
				// draw an X in the middle to indicate that this patch
				// has been eaten from
				strokeWeight(4.0);
				
				line( cell.x + 10, cell.y - 10, cell.x - 10, cell.y + 10 );
				
				// render left over amount
				/*
				String txt = "" + (int) foodLeft;
				textFont(theFont);
				textAlign(CENTER, TOP);
				
				float tW = textWidth(txt);
				stroke(0);
				strokeWeight(1.0);
				
				fill(#FF9912, 128);
				rect( cell.x - tW / 2.0 - 5.0, cell.y + 15.0, tW + 10.0, 16.0 + 10.0 );
				
				fill(0);
				text( txt, cell.x, cell.y + 20 );
				*/
			}
		}
		popStyle();
	}
  
  /**
    * Bunny eating state
    */
  public void setEating() {
    if( isArduinoConnected == true ) {
      port.write('A');
      port.write(id);
      println("Eating from: " + id);
    }
    state = 0;
  }
  
  /**
    * Bunny Ate state
    */
  public void setAte() {
    if( isArduinoConnected == true ) {
      port.write('E');
      port.write(id);
    }
    state = 1;
  }
  
  /**
    * Idle state
    */
  public void setIdle() {
    if( isArduinoConnected == true ) {
      port.write('I');
      port.write(id);
    }
    state = 2;
  }
}
